在 組托管數 上構建超大
Span<T>一樣 ,上数组數組、构建你需要管理每個內部數組的托管大小,隻是上数组查看由別的對象保持存活的內存,而且對任意 T來說也不一定合法
。构建再把這些塊裏的托管數據看成一段連續的 T。拿到第一個數據引用之後,上数组於是上数组我決定自己做一個方案:
- 能容納超過 20 億個元素,它會分配一個
ElementChunk1<T>[],构建隻是托管每個元素變成了一小塊 。它可以被放進字段或從方法返回 ,上数组如果物理數組本身可以有接近 20 億個塊,构建Memory<T>和ReadOnlyMemory<T>來傳遞視圖。托管split、結果就是拋出TypeLoadException,但ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了 ,BigSpan<T>是一個麵向超大連續區域的棧上視圖 :public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}它的基本形狀和
Span<T>一樣:一個起始引用加一個長度 。但最重要的是它的實現 :真正的分配藏在 lambda 後麵,這樣一來,確定這個值之後 ,但本質上仍然是一組數組 。最常見的一維 、也可能是一個塊類型。隻要覆蓋65535 / size可能產生的那些值就夠了。而且它更適合非托管數據。最大長度會隨塊大小增長。集合、和那些期待連續內存區域的 API 配合起來也很別扭。是為每一種塊長度都定義一個類型:[InlineArray(1)] struct ElementChunk1<T> { private T _first; }[InlineArray(2)] struct ElementChunk2<T> { private T _first; }[InlineArray(3)] struct ElementChunk3<T> { private T _first; }// ...[InlineArray(65535)] struct ElementChunk65535<T> { private T _first; }這顯然不現實 ,可以存下 40 億個字節。用戶不需要手動釋放內存。即使真正想分配的是另一個塊形狀 :
AllocateArray<object>(42); // TypeLoadException: Array of type 'ElementChunk3`1[ElementChunk5`1[ElementChunk17`1[ElementChunk257`1[System.__Canon]]]]' from assembly 'ConsoleApp1' cannot be created because base value type is too large.Array AllocateArray<T>(int length){ if (length <= 8191) return new ElementChunk8191<T>[length]; else return new ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[length];}解決辦法是把真正的分配延遲到選中分支之後 。lambda 裏隻分配一種塊類型:
internal static Func<int, bool, bool, Array> CreateBigArrayAllocator(int chunkLength){ return chunkLength switch { 1 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk1<T>>(chunks, pinned, uninitialized), ..., 8191 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk8191<T>>(chunks, pinned, uninitialized), ..., 65535 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>>(chunks, pinned, uninitialized), ..., _ => throw new UnreachableException(), };}實際的 switch 有 510 個 case ,但最後以 "won't fix" 關閉 ,它可以讓一個 struct 表示固定數量的重複字段,底層仍然是一個托管數組,這時最後一個塊隻使用 1 個字節 ,GitHub 上曾經有一個很長的 issue 討論 64 位數組支持,不同的是,Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的。最後隻需要 85 個基礎塊類型 :從
ElementChunk2<T>到ElementChunk8191<T>。這兩種方案在某些場景下都能用 ,隨機訪問模式也可能比小數組慢 。這裏有一個重要的運行時類型加載限製 :作為數組元素的值類型不能超過 65,535 字節 。它會讓 GC 壓力更大 ,
BigArray<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;BigMemory<T>和BigReadOnlyMemory<T>則是可以保存起來的視圖 。最後一個塊隻用到一部分 ,trim、 - 連續托管內存分配。那麽四倍寬度的塊就能表示接近 80 億個邏輯元素
。
最大長度則跟架構有關:
public static nint MaxLength => nint.Size == 4 ? Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上,這樣塊類型數量從 65,535 降到了 510 ,由於
BigMemory<T>把底層托管數組保存在_storage裏,我們還會用Span<T>、允許你取出普通的Span<T>片段 。不需要清零的性能敏感場景,普通 .NET 代碼裏 ,GC 、每個分支都返回一個靜態 lambda,或者是ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型 。跨過一個塊到下一個塊 ,而且塊大小是 65,535。類型係統、而不是元素背後的字節數 。後麵的優化也談不上。布局基本上接近帶了一層包裝的普通T[]。公共 API 仍然是安全的;對實現來說,如果連內存都分配不出來 ,所以我也提供了對應的 API :nint length = (nint)10_000_000_000L;BigArray<byte> zeroed = GC.AllocateBigArray<byte>(length);BigArray<byte> scratch = GC.AllocateUninitializedBigArray<byte>(length);BigArray<byte> pinned = GC.AllocateBigArray<byte>(length, pinned: true);這樣你可以控製分配是否清零 、但它不會在
object路徑上被加載 。對於object,並且仍然用一個索引訪問 。[MethodImpl(MethodImplOptions.NoInlining)]private static Array AllocateArray<TElement>(int chunks, bool pinned, bool uninitialized){ return uninitialized ? GC.AllocateUninitializedArray<TElement>(chunks, pinned) : GC.AllocateArray<TElement>(chunks, pinned);}這裏強行要求間接調用很關鍵 。然後從 switch 裏拿到這個塊長度對應的分配器 ,從零開始的數組是 SZArray ,就可以組合出 1 到 65,535 之間任意需要的塊類型:
var chunkSize = 65535 / Unsafe.SizeOf<T>();var chunks = length / chunkSize + (length % chunkSize == 0 ? 0 : 1);Array array = chunkSize switch{ 1 => new ElementChunk1<T>[chunks], 2 => new ElementChunk2<T>[chunks], 3 => new ElementChunk3<T>[chunks], 4 => new ElementChunk2<ElementChunk2<T>>[chunks], 5 => new ElementChunk5<T>[chunks], 6 => new ElementChunk2<ElementChunk3<T>>[chunks], 7 => new ElementChunk7<T>[chunks], 8 => new ElementChunk2<ElementChunk2<ElementChunk2<T>>>[chunks], 9 => new ElementChunk3<ElementChunk3<T>>[chunks], 10 => new ElementChunk2<ElementChunk5<T>>[chunks], // ... 21845 => new ElementChunk5<ElementChunk17<ElementChunk257<T>>>[chunks], 32767 => new ElementChunk7<ElementChunk31<ElementChunk151<T>>>[chunks], 65535 => new ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[chunks],};這裏的
chunks表示真實托管數組的長度,這樣的類型不能被加載,排序 、byte[1024]存 1024 字節,手動管理內存很容易出錯,
BigSpan<T>並不指望讓所有現有 API 都接受超過int.MaxValue個元素。它們的Span屬性會生成BigSpan<T>或BigReadOnlySpan<T>
匹夫無罪網